package com.ynunicom.dc.dingdingcontractapp.utils;

import com.ynunicom.dc.dingdingcontractapp.dto.exception.ParamNullException;
import lombok.SneakyThrows;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

/**
 * @author: jinye.Bai
 * @date: 2020/6/1 15:51
 */
public class DownloadHelper {

    @SneakyThrows
    public static boolean download(String url, String fileName){
        if (url==null||url.isEmpty()){
            throw new ParamNullException("url为空");
        }
        try {
            doDownload(url,fileName,3);
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    @SneakyThrows
    public static boolean download(String url){
        if (url==null||url.isEmpty()){
            throw new ParamNullException("url为空");
        }
        String fileName = Splitor(url);
        try {
            doDownload(url,fileName,3);
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    @SneakyThrows
    public static boolean download(String url, String fileName, int timeOut){
        if (url==null||url.isEmpty()){
            throw new ParamNullException("url为空");
        }
        try {
            doDownload(url,fileName,timeOut);
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    @SneakyThrows
    public static boolean download(String url, int timeOut){
        if (url==null||url.isEmpty()){
            throw new ParamNullException("url为空");
        }
        String fileName = Splitor(url);
        try {
            doDownload(url,fileName,timeOut);
        } catch (IOException e) {
            return false;
        }
        return true;
    }


    private static void doDownload(String url, String fileName,int timeOut) throws IOException {
        URL downLoadURL = new URL(url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) downLoadURL.openConnection();
        httpURLConnection.setConnectTimeout(timeOut*1000);
        httpURLConnection.connect();
        InputStream inputStream = httpURLConnection.getInputStream();
        byte[] buffer = new byte[inputStream.available()];
        inputStream.read(buffer);
        inputStream.close();
        File file = new File(fileName);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write(buffer);
        fileOutputStream.flush();
        fileOutputStream.close();
        httpURLConnection.disconnect();
    }

    private static String Splitor(String url){
        String[] strings = url.split("\\?");
        if (strings.length < 1){
            return UUID.randomUUID().toString().replaceAll("-","");
        }
        String[] innerStrings = strings[0].split("/");
        if (innerStrings.length < 1){
            return UUID.randomUUID().toString().replaceAll("-","");
        }
        return innerStrings[innerStrings.length-1];
    }
}
